#include "music.h"
#include "ui_music.h"
#include <QMouseEvent>
#include <QGraphicsDropShadowEffect>

Music::Music(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Music)
{
    ui->setupUi(this);
    init();

    connectSingalAndSlot();
}

Music::~Music()
{
    delete ui;
}

void Music::init()
{
    //设置窗口为无框窗口
    this->setWindowFlag(Qt::FramelessWindowHint);

    //设置窗口背景透明
    setAttribute(Qt::WA_TranslucentBackground,true);

    //设置窗口阴影效果
    QGraphicsDropShadowEffect* shadow=new QGraphicsDropShadowEffect(this);
    shadow->setOffset(0,0);//阴影偏移量
    shadow->setColor("#000000");
    shadow->setBlurRadius(20);//设置模糊半径
    this->setGraphicsEffect(shadow);

    //为自定义按钮ButtonForm设置图标和文本
    ui->rec->setIconAndText(":/images/rec.png","推荐",0);
    ui->radio->setIconAndText(":/images/radio.png","电台",1);
    ui->music->setIconAndText(":/images/music.png","音乐馆",2);
    ui->like->setIconAndText(":/images/like.png","我的喜欢",3);
    ui->local->setIconAndText(":/images/local.png","本地音乐",4);
    ui->recent->setIconAndText(":/images/recent.png","最近播放",5);

    //默认显示本地音乐
    ui->local->showLineStyle();
    ui->stackedWidget->setCurrentIndex(4);
    ui->local->setLocalStyle();//设置音符跳动效果
}

void Music::connectSingalAndSlot()
{
    //ButtonForm切换页面的信号与槽函数绑定
    connect(ui->rec,&ButtonForm::btClick,this,&Music::onBtFormClick);
    connect(ui->radio,&ButtonForm::btClick,this,&Music::onBtFormClick);
    connect(ui->music,&ButtonForm::btClick,this,&Music::onBtFormClick);
    connect(ui->like,&ButtonForm::btClick,this,&Music::onBtFormClick);
    connect(ui->local,&ButtonForm::btClick,this,&Music::onBtFormClick);
    connect(ui->recent,&ButtonForm::btClick,this,&Music::onBtFormClick);
}

void Music::onBtFormClick(int PageID)
{
    //颜色修改,将PageID之外的ButtonForm的stylesheet恢复原来的状态
    //获取所有的ButtonForm
    QList<ButtonForm*> buttonFormList=this->findChildren<ButtonForm*>();
    for(auto& it:buttonFormList)
    {
        if(it->getID()!=PageID)
        {
            it->clearStyle();
        }
    }
    //切换页面
    ui->stackedWidget->setCurrentIndex(PageID);
}

void Music::mouseMoveEvent(QMouseEvent *event)
{
    if(Qt::LeftButton==event->buttons())
    {
        move(event->globalPos()-dragPosition);
        return;
    }
    QWidget::mouseMoveEvent(event);
}

void Music::mousePressEvent(QMouseEvent *event)
{
    if(Qt::LeftButton==event->button())
    {
        //获取鼠标与widget的相对位置
        dragPosition=event->globalPos()-geometry().topLeft();
        return;
    }
    QWidget::mousePressEvent(event);
}


void Music::on_quit_clicked()
{
    close();
}
